<?php
/**
 * Created by glq.
 * User: lenovo
 * Date: 2019/8/8
 * Time: 22:33
 */

namespace Portal\Controller;

use Common\Controller\HomebaseController;
use Portal\Model\PostsModel;

class RssController extends HomebaseController {

//    channel
    public $items;
    public $tags;
    public $pubDate;
    public $title;
    public $link;
    public $description;
    public $language = "zh-cn";

    public function __construct()
    {
        $this->items = array();
        $this->tags  = array();
    }

    public function index() {
        $p = I('get.p', 1);
        $this->title = "日新网首页";
        $this->link = "https://www.rixins.com";
        $this->description = "日新网最新文章";

        $lists = M('posts')
            ->where(['post_type' => PostsModel::POST_TYPE_ARTICLE, 'post_status' => PostsModel::POST_STATUS_NORMAL])
            ->limit(($p - 1) * 20, 20)
            ->order('id desc')
            ->select();

        foreach ($lists as $v) {
            $item = new RssItem();
            $item->item_title   = $v['post_title'];
            $item->item_link    = C("DOMAIN_NAME") . U('Portal/Article/article_info', ['id' => $v['id']]);
            $item->item_set_pub_date($v['post_modified']);
            $item->item_description = "<![CDATA[" . $v['post_excerpt'] . "]]>";
            $item->item_guid =  $v['id'];
            $this->add_item($item);
        }
        echo $this->serve();
    }


    public function add_item($item)
    {
        $this->items[] = $item;
    }

    public function set_pub_date($when)
    {
        if(strtotime($when) == false)
            $this->pubDate = date("D, d M Y H:i:s ", $when) . "GMT";
        else
            $this->pubDate = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
    }

    private function get_pub_date()
    {
        if(empty($this->pubDate))
            return date("D, d M Y H:i:s ") . "GMT";
        else
            return $this->pubDate;
    }

    public function add_tag($tag, $value)
    {
        $this->tags[$tag] = $value;
    }

    private function out()
    {
        $out  = $this->header();
        $out .= "<channel>\n";
        $out .= "<title>" . $this->title . "</title>\n";
        $out .= "<link>" . $this->link . "</link>\n";
        $out .= "<description>" . $this->description . "</description>\n";
        $out .= "<copyright>" . "Copyright © 2018-2019 日新网 www.rixins.com All Rights Reserved. " . "</copyright>\n";
        $out .= "<docs>" . "www.rixins.com" . "</docs>\n";
        $out .= "<language>" . $this->language . "</language>\n";
        $out .= "<pubDate>" . $this->get_pub_date() . "</pubDate>\n";
        $out .= "<ttl>60</ttl>";

        foreach($this->tags as $key => $val) $out .= "<$key>$val</$key>\n";
        foreach($this->items as $item) $out .= $item->item_out();

        $out .= "</channel>\n";

        $out .= $this->footer();

        $out = str_replace("&", "&amp;", $out);

        return $out;
    }

    public function serve($contentType = "application/xml")
    {
        $xml = $this->out();
        header("Content-type: $contentType");
        echo $xml;
    }

    private function header()
    {
        $out  = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
        $out .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";
        return $out;
    }

    private function footer()
    {
        return '</rss>';
    }
}

class RssItem {

    public $item_title;
    public $item_link;
    public $item_description;
    public $item_pub_date;
    public $item_guid;
    public $item_tags;
    public $item_attachment;
    public $item_length;
    public $item_mimetype;

    public function __construct()
    {
        $this->item_tags = array();
    }


    public function item_set_pub_date($when)
    {
        if(strtotime($when) == false)
            $this->item_pub_date = date("D, d M Y H:i:s ", $when) . "GMT";
        else
            $this->item_pub_date = date("D, d M Y H:i:s ", strtotime($when)) . "GMT";
    }

    private function item_get_pub_date()
    {
        if(empty($this->item_pub_date)) {
            return date("D, d M Y H:i:s ") . "GMT";
        } else {
            return $this->item_pub_date;
        }
    }

    public function item_add_tag($tag, $value)
    {
        $this->item_tags[$tag] = $value;
    }

    public function item_out()
    {
        $out = "";
        $out .= "<item>\n";
        $out .= "<title>" . $this->item_title . "</title>\n";
        $out .= "<link>" . $this->item_link . "</link>\n";
        $out .= "<description>" . $this->item_description . "</description>\n";
        $out .= "<pubDate>" . $this->item_get_pub_date() . "</pubDate>\n";

        if($this->item_attachment != "")
            $out .= "<enclosure url='{$this->item_attachment}' length='{$this->item_length}' type='{$this->item_mimetype}' />";

        if(empty($this->item_guid)) $this->item_guid = $this->item_link;
        $out .= "<guid>" . $this->item_guid . "</guid>\n";

        foreach($this->item_tags as $key => $val) $out .= "<$key>$val</$key\n>";
        $out .= "</item>\n";
        return $out;
    }

    public function enclosure($url, $mimetype, $length)
    {
        $this->item_attachment = $url;
        $this->item_mimetype   = $mimetype;
        $this->item_length     = $length;
    }
}